var express = require('express');
var router = express.Router();
var dataCtrl = require("../controllers/user.controller");
var jwt=require('jsonwebtoken');

function verifyToken(req,res,next){
    const bearerHeader=req.headers['authorization'];
    //console.log(bearerHeader);
    if(typeof bearerHeader !== 'undefined'){
        const bearer=bearerHeader.split(' ');
        const bearerToken=bearer[1];
       // req.token=bearerToken;
        //console.log(bearerToken);
        jwt.verify(bearerToken,'mySecretString',function(err,decode){
            console.log(decode);
            if(decode !== undefined){
                next();
            }else{
                res.sendStatus(403);
            }

        });
    }else{
        res.sendStatus(403)
    }
}

/* GET users listing. */
router.post('/', dataCtrl.create); //增加
router.put('/:id', dataCtrl.update); //修改
router.delete('/:id', dataCtrl.delete); //单条删除
router.get('/:id', dataCtrl.selectbyId); //根据id查找
//自定义的中间件 verifyToken
router.post('/list',verifyToken, dataCtrl.list); //分页（10条为一页）--查找多条记录
router.post('/deletes', dataCtrl.deletes); //多条删除
router.post('/login',dataCtrl.login);

module.exports = router;